﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;
using System.Text.RegularExpressions;
using Portal.CMS;


namespace Portal.CDSP
{
    public class PubAdminWP : WebPart
    {
        private const int MaxString = 40;
        public override string WebPartId
        {
            get
            {
                return "WebSite.PubAdminWP";
            }
        }


        public override string WebPartFolder
        {
            get
            {
                return "Portal.CDSP";
            }
        }

        public override string Title
        {
            get
            {
                return GetText("FTitle", "SOYT-Nhóm tin hành chính công");
            }
        }


        public override ViewCls[] Views
        {
            get
            {
                return new ViewCls[]{                    
                    new ViewCls("Default",GetText("Default","Hiển thị mặc định"),1),                             
                   };
            }
        }

        public override string NameSpace
        {
            get
            {
                return "Portal.CDSP";
            }
        }


        public override string ClassName
        {
            get
            {
                return "PubAdminWP";
            }
        }


        public override RenderInfoCls ClientRenderWebPartWithInfo(string BelongSite, string TabModuleId, string ViewName, string HasPorlet, LoadParamCls loadParamCls)
        {
            RenderInfoCls oRenderInfoCls = new RenderInfoCls();

            try
            {
                if (ViewName.Equals("Default"))
                {
                    oRenderInfoCls.Content = ClientRenderWebPartDefault(TabModuleId, HasPorlet, ref oRenderInfoCls.ModuleTitle);
                }
                return oRenderInfoCls;
            }
            catch (Exception ex)
            {
                oRenderInfoCls.ErrorMsg = ex.Message.ToString();
                oRenderInfoCls.Error = true;
                return oRenderInfoCls;
            }
        }

        public override DataTable ReadingDataService(string belongSite, string ActiveLanguage, ref string IdField, ref string SubjectField)
        {
            return new FSpecialSubjectBLL().ReadingForServiceForPubAdmin(belongSite, ActiveLanguage);
        }

        // Nhóm tin quản lý nhà nước về yte
        private string ClientRenderWebPartDefault(string TabModuleId, string HasPorlet, ref string ModuleTitle)
        {
            StringBuilder Html = new StringBuilder();
            try
            {
                TabModuleCls TabModuleCls = new TabModuleBLL().CreateModelFromXml(TabModuleId);
                if (TabModuleCls == null) throw new Exception("[" + TabModuleId + "] " + GetText("TabModuleIdNull", "not found!"));

                FSpecialSubjectCls FSubjectCls = new FSpecialSubjectBLL().CreateModelItem(TabModuleCls.Id, TabModuleCls.belongSite);
                string GroupSubject = FSubjectCls.GroupNewsItemName;
                string GroupNewsItemId = FSubjectCls.GroupNewsItemId;
                string belongSite = TabModuleCls.belongSite;
                string NewsByCategoryUrl = Environments.GetNewsCategoryUrl(belongSite, GroupNewsItemId, GroupSubject, "_RunTime");

                int LimitRow;
                try
                {
                    LimitRow = int.Parse(Environment.Environments.GetAppSettings("PubAdmin"));
                }
                catch { LimitRow = 3; }

                int PubAdminNum = LimitRow;

                DataTable dtSubCat = new FSpecialSubjectBLL().ReadingSubCatMenuItems(GroupNewsItemId);

                Html.Append(
                    "   <div id=\"tabs\">\n" +
                    "				<ul>\n");
                string[] arrSubCat = new string[dtSubCat.Rows.Count];

                for (int iSub = 0; iSub < dtSubCat.Rows.Count; iSub++)
                {
                    string SubGroupNewsItemId = (string)dtSubCat.Rows[iSub]["GroupNewsItemId"];
                    string SubGroupNewsItemName = (string)dtSubCat.Rows[iSub][Environments.ActiveLanguage + "GroupNewsItemName"];

                    Html.Append("<li>" + SubGroupNewsItemName + "</li>\n");
                    arrSubCat[iSub] = SubGroupNewsItemId;
                }

                Html.Append(
                    "</ul>\n");
                if (arrSubCat.Length == 0) Html.Append("</div>");

                for (int iSubCat = 0; iSubCat < arrSubCat.Length; iSubCat++)
                {
                    DataTable dtNews = new FSpecialSubjectBLL().ReadingNews_ChuyenMuc(arrSubCat[iSubCat], PubAdminNum, belongSite);
                    if (dtNews.Rows.Count == 0)
                    {
                        Html.Append("<div></div>");
                        continue;
                    }

                    if (PubAdminNum > dtNews.Rows.Count)
                    {
                        PubAdminNum = dtNews.Rows.Count;
                    }
                    string subCatUrl = string.Empty;
                    string SubGroupNewsItemId = (string)dtSubCat.Rows[iSubCat]["GroupNewsItemId"];
                    string SubGroupNewsItemName = (string)dtSubCat.Rows[iSubCat][Environments.ActiveLanguage + "GroupNewsItemName"];
                    int IsGroupNews = (int)dtSubCat.Rows[iSubCat]["IsGroupNews"];

                    if (IsGroupNews == 1)
                        subCatUrl = Environments.GetNewsCategoryUrl(TabModuleCls.belongSite, SubGroupNewsItemId, SubGroupNewsItemName, "_RunTime");
                    else
                        subCatUrl = Environments.GetStaticHtmlUrl(TabModuleCls.belongSite, SubGroupNewsItemId, SubGroupNewsItemName);

                    Html.Append(
                        "				<div>\n" +
                        "					<ul>\n"
                        );
                    for (int iIndexNews = 0; iIndexNews < PubAdminNum; iIndexNews++)
                    {
                        string ShortId = (string)dtNews.Rows[iIndexNews]["ShortId"];
                        string Title = "";
                        try
                        {
                            Title = (string)dtNews.Rows[iIndexNews][Environments.ActiveLanguage + "Subject"];
                        }
                        catch { }

                        string Description = "";
                        try
                        {
                            Description = (string)dtNews.Rows[iIndexNews][Environments.ActiveLanguage + "ShortContent"];
                        }
                        catch { }

                        try
                        {
                            DateTime EntryDate = (DateTime)dtNews.Rows[iIndexNews]["EntryDate"];
                        }
                        catch { }

                        string Url = Environments.GetNewsUrl(belongSite, ShortId, Title, "_runtime");
                        Description = Ultility.ClearHTMLTags(Description);
                        Html.Append(
                                    "<li><a href=\"" + Url + "\">" + Title + "</a></li>\n");

                    }
                    Html.Append(
                        "					</ul>\n" +
                        "					<a href=\"" + subCatUrl + "\">Xem toàn bộ</a>\n" +
                        "				</div>\n");

                    PubAdminNum = LimitRow;

                }

                Html.Append(
                    "				<a class=\"banner-in-tab\" href=\"#\"><img src=\"img/hcc.png\" /></a>\n"
                    );
                Html.Append(
                    "</div>");
            }
            catch (Exception ex)
            {
                Html.Append("ERROR:" + ex.Message);
            }
            return Html.ToString();
        }

        private string FormatDesc(string Description)
        {
            int Count = 0;
            int j = 0;
            Description = Regex.Replace(Description, @"<(.|\n)*?>", string.Empty);
            int Length = Description.Length;
            for (int i = 1; i < Description.Length; i++)
            {
                if (Description[i - 1].ToString().Equals(" ") && !Description[i].ToString().Equals(" "))
                    Count++;
                if (Count <= MaxString)
                {
                    j = i;
                }
            }
            if (Count > MaxString)
            {
                Description = Description.ToString().Remove(j, (Length - j));
                Description = string.Concat(Description, "...");
            }
            return Description;
        }

        [AjaxPro.AjaxMethod()]
        public string ServerSideCallRssChannel(string GroupNewsId, string belongSite, string GroupSubject)
        {
            try
            {
                var RSSChannel = new RSimpleSyndicationBLL().MakeXmlDataForChanel(GroupSubject, "RSS-CDSPNA Website", "http://cdspna.edu.vn", "CDSPNA", "CDSPNA", DateTime.Now, DateTime.Now, GroupNewsId, "xml", belongSite);
                return RSSChannel.Url;

            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message;
            }
        }

        public string ShowTooltip(string Tooltip)
        {
            string[] charsToTrim = { "'", "[", "]", "*", "\'", ";", ",", ".", "#", "@", "%", };//!@#$%^&*()+=-[]\';,./{}|\":<>?
            for (int i = 0; i < charsToTrim.Length; i++)
            {
                if (Tooltip.IndexOf(charsToTrim[i]) != -1)
                {
                    Tooltip.Replace(charsToTrim[i], "\"" + charsToTrim[i] + "\"");
                }
            }
            return Tooltip;
        }

        public override bool AllowFrontService
        {
            get
            {
                return true;
            }
        }

        public override void RegAjaxPro(System.Web.UI.Page ActivePage)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(FCategoryHomeWP), ActivePage);
        }

    }
}
